package com.example.demo.use_demo.a_file_stream;

import java.io.*;

/**
 * @author farben
 */
public class FileStreamTransferUtil {

    public static void transfer1() throws Exception {
        // InputStream => OutputStream
        InputStream inputStream = new FileInputStream("xxx.txt");

        OutputStream outputStream = new FileOutputStream("xxx.txt");
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] byteArr = new byte[1024];
        int length;
        while ((length = inputStream.read(byteArr)) != -1){
            byteArrayOutputStream.write(byteArr,0,length);
            outputStream.write(byteArr,0,length);
        }

        byteArrayOutputStream.writeTo(outputStream);

        // OutputStream => InputStream
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
        outputStream.write(byteArrayOutputStream.toByteArray());

        while ((length = byteArrayInputStream.read(byteArr)) != -1){
            byteArrayOutputStream.write(byteArr,0,length);
        }
    }

}
